﻿using System;
using System.Web.Mvc;
using BrettM.Helpers;
using BrettM.Restful.SDK;

namespace BrettM.Web.Restful
{
    public abstract class RestController
    : Controller, IRestController
    {

        protected internal RestController()
        {
        }

        public virtual ActionResult Help()
        {
            var info = this.GetRESTfulAttributes();

            return View("Help", info);
        }

        public virtual string GetResourceName()
        {
            string controllerName = this.GetType().Name;
            if (controllerName.EndsWith("Controller", StringComparison.InvariantCultureIgnoreCase))
            {
                controllerName = controllerName.Substring(0, controllerName.Length - 10);
            }

            return controllerName.ToLower().ToPlural();
        }

        public virtual string GetResourceItemName()
        {
            return GetResourceItemName("{key}");
        }


        public virtual string GetResourceItemName(string child)
        {
            return string.IsNullOrEmpty(child) ? GetResourceName() : string.Format("{0}/{1}", GetResourceName(), child);
        }

    }
}
